package org.csu.logistics.Service;

import org.csu.logistics.common.CommonResponse;
import org.csu.logistics.entity.Order;
import org.csu.logistics.entity.Profit;
import org.csu.logistics.entity.TransCenter;
import org.csu.logistics.entity.Warehouse;
import org.csu.logistics.persistence.OrderMapper;
import org.csu.logistics.persistence.ProfitMapper;
import org.csu.logistics.persistence.TransCenterMapper;
import org.csu.logistics.persistence.UnitPriceMapper;
import org.csu.logistics.vo.OrderOV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@Service("OrderOVService")
public class OrderOVServiceImp implements OrderOVService{

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    ProfitMapper profitMapper;

    @Autowired
    UnitPriceMapper unitPriceMapper;

    @Autowired
    OrderService orderService;

    @Autowired
    TransCenterService transCenterService;

    @Autowired
    WarehouseService warehouseService;

    @Override
    public OrderOV insert_OrderOV(OrderOV orderOV) {

        orderMapper.insert(orderVOToOrder(orderOV));

        String origin = orderOV.getOrigin();
        TransCenter transCenter = transCenterService.select_transCenterByLocale(origin);
        int id = transCenter.getWarehouseId();

        String destination = orderOV.getDestination();
        TransCenter transCenter1 = transCenterService.select_transCenterByLocale(destination);
        int id1 = transCenter1.getWarehouseId();

        String way = "";
        if(id1==1 && id!=1) {
            way = transCenter.getFirst();
        }
        if(id1==2 && id!=2) {
            way = transCenter.getSecond();
        }
        if(id1==3 && id!=3) {
            way = transCenter.getThird();
        }
        if(id1==4 && id!=4) {
            way = transCenter.getFourth();
        }
        if(id1==5 && id!=5) {
            way = transCenter.getFifth();
        }
        if(id1==6 && id!=6) {
            way = transCenter.getSixth();
        }
        if(id1==7 && id!=7) {
            way = transCenter.getSeventh();
        }
        if(id1==8 && id!=8) {
            way = transCenter.getEighth();
        }


        if(!way.equals("")){
            String[] Tokens = way.split("[|]");
            if(orderOV.getTransportMode().equals("标准快递")){
                double shippingPrice = Double.parseDouble(Tokens[Tokens.length-1])/100;
                double costPrice = shippingPrice * 0.6;
                orderOV.setShippingPrice(shippingPrice);
                orderOV.setCostPrice(costPrice);
            }
            if(orderOV.getTransportMode().equals("特准快件")){
                double shippingPrice = Double.parseDouble(Tokens[Tokens.length-1])/100 * 2;
                double costPrice = shippingPrice * 0.7;
                orderOV.setShippingPrice(shippingPrice);
                orderOV.setCostPrice(costPrice);
            }
            if(orderOV.getTransportMode().equals("大件快递")){
                double shippingPrice = Double.parseDouble(Tokens[Tokens.length-1])/100 * 3;
                double costPrice = shippingPrice * 0.8;
                orderOV.setShippingPrice(shippingPrice);
                orderOV.setCostPrice(costPrice);
            }

            Profit profit = new Profit();
            Order order = orderService.select_orderByUserId(orderOV.getUserId());
            profit.setOrderNum(order.getOrderNum());
            profit.setCostPrice(orderOV.getCostPrice());
            profit.setShippingPrice(orderOV.getShippingPrice());
            profitMapper.insert(profit);
            orderOV.setOrderNum(order.getOrderNum());
        }
        return orderOV;
    }

    @Override
    public void delete_OrderOVByOrderNum(int orderNum) {

    }

    @Override
    public void update_OrderOVByOrderNum(OrderOV orderOV) {
        Order order = orderVOToOrder(orderOV);
        order.setOrderNum(orderOV.getOrderNum());
        orderMapper.updateById(order);
        Profit profit = new Profit();
        profit.setOrderNum(orderOV.getOrderNum());
        profit.setCostPrice(orderOV.getCostPrice());
        profit.setShippingPrice(orderOV.getShippingPrice());
        profitMapper.updateById(profit);
    }

    @Override
    public OrderOV select_OrderOVByOrderNum(int orderNum) {

        Order order = orderService.select_orderByOrderNum(orderNum);
        OrderOV orderOV = orderToOrderOV(order);
        orderOV.setOrderNum(orderNum);
        Profit profit = new Profit();
        profit = profitMapper.selectById(orderNum);
        orderOV.setShippingPrice(profit.getShippingPrice());
        orderOV.setCostPrice(profit.getCostPrice());
        return orderOV;
    }

    @Override
    public CommonResponse<List<OrderOV>> select_OrderOVList(int userId){
        List<Order> orderList = orderService.select_orderListByUserId(userId);
        if(orderList==null){
            return CommonResponse.createForError("该用户没有订单");
        }
        else{
            List<OrderOV> orderOVList = new ArrayList<>();
            for (Order order : orderList) {
                Profit profit = profitMapper.selectById(order.getOrderNum());
                OrderOV orderOV = orderToOrderOV(order);
                orderOV.setShippingPrice(profit.getShippingPrice());
                orderOV.setCostPrice(profit.getCostPrice());
                orderOV.setOrderNum(order.getOrderNum());
                orderOVList.add(orderOV);
            }
            return CommonResponse.createForSuccess(orderOVList);
        }
    }


    @Override
    public CommonResponse<List<OrderOV>> select_OrderOVListByUserId(int userId) {

        List<Order> orderList = orderService.select_orderListByUserId(userId);
        if(orderList==null){
            return CommonResponse.createForError("该用户没有运输中的订单");
        }
        else{
            List<OrderOV> orderOVList = new ArrayList<>();
            for (Order order : orderList) {
                if (order.getOrderStatus().equals("运输中")) {
                    Profit profit = profitMapper.selectById(order.getOrderNum());
                    OrderOV orderOV = orderToOrderOV(order);
                    orderOV.setShippingPrice(profit.getShippingPrice());
                    orderOV.setCostPrice(profit.getCostPrice());
                    orderOV.setOrderNum(order.getOrderNum());
                    orderOVList.add(orderOV);
                }

            }

            return CommonResponse.createForSuccess(orderOVList);
        }
    }

    @Override
    public CommonResponse<List<OrderOV>> select_OrderOVListByOrderStatus(String orderStatus){

        List<Order> orderList = orderService.select_orderListByOrderStatus(orderStatus);
        if(orderList==null){
            return CommonResponse.createForError("没有"+orderStatus+"的订单");
        }
        else{
            List<OrderOV> orderOVList = new ArrayList<>();
            for(int i=0;i<orderList.size();i++){
                Profit profit = profitMapper.selectById(orderList.get(i).getOrderNum());
                OrderOV orderOV = orderToOrderOV(orderList.get(i));
                orderOV.setShippingPrice(profit.getShippingPrice());
                orderOV.setCostPrice(profit.getCostPrice());
                orderOV.setOrderNum(orderList.get(i).getOrderNum());
                orderOVList.add(orderOV);
            }

            return CommonResponse.createForSuccess(orderOVList);
        }
    }


    //    @Override
//    public CommonResponse<List<OrderOV>> select_OrderOVListByOrderStatus(String)
    public Order orderVOToOrder(OrderOV orderOV){

        Order order = new Order();
        order.setUserId(orderOV.getUserId());
        order.setOrigin(orderOV.getOrigin());
        order.setDestination(orderOV.getDestination());
        order.setTransportMode(orderOV.getTransportMode());
        order.setWeight(orderOV.getWeight());
        order.setOrderStatus(orderOV.getOrderStatus());
        order.setEvaluate(orderOV.getEvaluate());
        order.setRecipientsName(orderOV.getRecipientsName());
        order.setRecipientsTel(orderOV.getRecipientsTel());
        order.setSenderName(orderOV.getSenderName());
        order.setSenderTel(orderOV.getSenderTel());
        order.setAfterSale(orderOV.getAfterSale());
        return order;
    }

    public OrderOV orderToOrderOV(Order order){
        OrderOV orderOV = new OrderOV();
        orderOV.setOrderNum(orderOV.getOrderNum());
        orderOV.setUserId(order.getUserId());
        orderOV.setOrigin(order.getOrigin());
        orderOV.setDestination(order.getDestination());
        orderOV.setTransportMode(order.getTransportMode());
        orderOV.setWeight(order.getWeight());
        orderOV.setOrderStatus(order.getOrderStatus());
        orderOV.setEvaluate(order.getEvaluate());
        orderOV.setRecipientsName(order.getRecipientsName());
        orderOV.setRecipientsTel(order.getRecipientsTel());
        orderOV.setSenderName(order.getSenderName());
        orderOV.setSenderTel(order.getSenderTel());
        orderOV.setAfterSale(order.getAfterSale());

        return orderOV;
    }
}
